﻿using System;
using System.IO;
using System.ServiceModel.Channels;
using System.Text;
using System.Xml;

namespace PermitsAndVisasLeadsSyncher
{
    public class CustomTextMessageEncoder : MessageEncoder
    {
        private CustomTextMessageEncoderFactory factory;
        private XmlWriterSettings writerSettings;
        private string contentType;

        public override string ContentType
        {
            get
            {
                return this.contentType;
            }
        }

        public override string MediaType
        {
            get
            {
                return this.factory.MediaType;
            }
        }

        public override MessageVersion MessageVersion
        {
            get
            {
                return this.factory.MessageVersion;
            }
        }

        public CustomTextMessageEncoder(CustomTextMessageEncoderFactory factory)
        {
            this.factory = factory;
            this.writerSettings = new XmlWriterSettings();
            this.writerSettings.Encoding = Encoding.GetEncoding(factory.CharSet);
            this.contentType = string.Format("{0}; charset={1}", (object)this.factory.MediaType, (object)this.writerSettings.Encoding.HeaderName);
        }

        public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType)
        {
            byte[] buffer1 = new byte[buffer.Count];
            Array.Copy((Array)buffer.Array, buffer.Offset, (Array)buffer1, 0, buffer1.Length);
            bufferManager.ReturnBuffer(buffer.Array);
            return this.ReadMessage((Stream)new MemoryStream(buffer1), int.MaxValue);
        }

        public override Message ReadMessage(Stream stream, int maxSizeOfHeaders, string contentType)
        {
            return Message.CreateMessage(XmlReader.Create(stream), maxSizeOfHeaders, this.MessageVersion);
        }

        public override ArraySegment<byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset)
        {
            MemoryStream memoryStream = new MemoryStream();
            XmlWriter writer = XmlWriter.Create((Stream)memoryStream, this.writerSettings);
            message.WriteMessage(writer);
            writer.Close();
            byte[] buffer = memoryStream.GetBuffer();
            int num = (int)memoryStream.Position;
            memoryStream.Close();
            int bufferSize = num + messageOffset;
            byte[] array = bufferManager.TakeBuffer(bufferSize);
            Array.Copy((Array)buffer, 0, (Array)array, messageOffset, num);
            return new ArraySegment<byte>(array, messageOffset, num);
        }

        public override void WriteMessage(Message message, Stream stream)
        {
            XmlWriter writer = XmlWriter.Create(stream, this.writerSettings);
            message.WriteMessage(writer);
            writer.Close();
        }

        public override bool IsContentTypeSupported(string contentType)
        {
            if (base.IsContentTypeSupported(contentType))
                return true;
            if (contentType.Length == this.MediaType.Length)
                return contentType.Equals(this.MediaType, StringComparison.OrdinalIgnoreCase);
            return contentType.StartsWith(this.MediaType, StringComparison.OrdinalIgnoreCase) && (int)contentType[this.MediaType.Length] == 59;
        }
    }
}
